<template>
    <div class="common-layout">
        <el-container>
            <!-- 头部内容开始 -->
            <el-header class="header-wrapped">
                <div class="header-content">
                    <h3>后台管理系统</h3>
                    <span class="welcome">欢迎您的登录</span>
                </div>
            </el-header>
            <!-- 头部内容结束 -->
            <!-- 中间部分开始 -->
            <el-main>
                <div class="login-wrapped">
                    <!-- card卡片 -->
                    <el-card class="box-card">
                        <el-tabs v-model="activeName" class="demo-tabs" :stretch="true">
                            <el-tab-pane label="登录" name="first">
                                <!-- 表单开始 -->
                                <el-form class="login-form">
                                    <el-form-item label="账号">
                                        <el-input v-model="loginData.account" placeholder="请输入账号" />
                                    </el-form-item>
                                    <el-form-item label="密码">
                                        <el-input v-model="loginData.password" placeholder="请输入密码" />
                                    </el-form-item>
                                    <div class="footer-wrapped">
                                        <!-- 忘记密码 -->
                                        <div class="forget-password">
                                            <span class="forget-password-button" @click="openForget">忘记密码</span>
                                        </div>
                                        <!-- 登录 -->
                                        <div class="footer-button">
                                            <el-button type="primary" @click="goLogin">登录</el-button>
                                        </div>
                                        <!-- 立即注册 -->
                                        <div class="footer-go-register">
                                            还没有账号？<span class="go-register">立即注册</span>
                                        </div>
                                    </div>
                                </el-form>
                                <!-- 表单结束 -->
                            </el-tab-pane>
                            <el-tab-pane label="注册" name="second">
                                <el-form class="register-form">
                                    <el-form-item label="账号">
                                        <el-input v-model="registerData.account" placeholder="账号长度6-12位" />
                                    </el-form-item>
                                    <el-form-item label="密码">
                                        <el-input v-model="registerData.password" placeholder="密码需长度6-12位含字母数字" />
                                    </el-form-item>
                                    <el-form-item label="确认密码">
                                        <el-input v-model="registerData.rePassword" placeholder="请再次输入密码" />
                                    </el-form-item>
                                    <!-- 注册 -->
                                    <div class="footer-button">
                                        <el-button type="primary" @click="goRegister">注册</el-button>
                                    </div>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </el-card>
                </div>
            </el-main>
            <!-- 中间部分结束 -->
            <!-- 底部开始 -->
            <el-footer class="footer-wrapped">
                <div class="footer-content">
                    <div class="title">
                        <span>阿里云博客专家</span> |
                        <span>阿里云博客专家</span> |
                        <span>阿里云博客专家</span>
                    </div>
                </div>
            </el-footer>
            <!-- 底部结束 -->
        </el-container>
    </div>
    <Forget ref="forgetP"></Forget>
</template>
<script lang="ts" setup>
import { ElMessage } from 'element-plus'
import { reactive, ref } from 'vue';
import Forget from './components/forget_password.vue'
import { useRouter } from 'vue-router';
const router = useRouter()
// 导入登录和注册接口
import { login, register } from '@/api/login'
// 导入pinia中的方法
import { useUserInfor } from '@/store/userinfor.js'

const store = useUserInfor()
// 忘记密码弹窗
const forgetP = ref()
// 表单接口
interface formData {
    account: string
    password: string
    rePassword?: string
}
const activeName = ref('first')
// 登录表单数据
const loginData: formData = reactive({
    account: '111111',
    password: 'w111111'
})
// 注册表单数据
const registerData: formData = reactive({
    account: '111111',
    password: 'w111111',
    rePassword: 'w111111'
})
// 打开忘记密码弹窗
const openForget = () => {
    forgetP.value.open()
}
// 注册按钮
const goRegister = async () => {
    if (registerData.password == registerData.rePassword) {
        const res = await register(registerData)
        console.log(res);
        
        if (res.message == "注册账号成功") {
            ElMessage({
                message: '注册成功',
                type: 'success',
            })
            activeName.value = 'first'
        } else if (res.message == "账号已经存在") {
            ElMessage.warning('账号已经存在')
        } else {
            ElMessage.error('注册失败，请检查数据是否正确')
        }
    } else {
        ElMessage.error('请再次输入密码')
    }

}

// 登录按钮的逻辑
const goLogin = async () => {
    const res = await login(loginData)
    // console.log(res);
    if (res.message == '登录成功') {
        ElMessage({
            message: '登录成功',
            type: 'success',
        })
        const { id } = res.results
        store.userInfor(id)
        localStorage.setItem('token', res.token)
        // localStorage.setItem('id', id)
        // localStorage.setItem('name', name)
        // localStorage.setItem('department', department)
        router.push('/home')
    } else {
        ElMessage.error('密码错误')
    }

}

</script>
<style lang="scss" scoped>
// 头部外壳
.header-wrapped {

    // 头部内容
    .header-content {
        width: 1200px;
        display: flex;
        justify-content: space-evenly;
        align-items: center;
        margin: 0 auto;

        // 欢迎语
        .welcome {
            font-size: 13px;
        }
    }
}

// 主体部分
.el-main {
    --el-main-padding: 0;
    background-image: url('@/assets/code.jpg');
    height: 600px;

    // 登录外壳
    .login-wrapped {
        width: 1200px;
        height: 600px;
        margin: 0 auto;

        // 卡片样式
        .box-card {
            width: 350px;
            height: 375px;
            float: right;
            position: relative;
            top: 14%;

            // 登录
            .footer-button {
                width: 100%;
                display: flex;
                justify-content: center;
            }

            // 表单边距
            .login-form {
                margin-top: 30px;

                .footer-wrapped {
                    display: flex;
                    flex-direction: column;

                    // 忘记密码开始
                    .forget-password {
                        display: flex;
                        justify-content: flex-end;
                        margin: 10px 0;

                        .forget-password-button {
                            font-size: 12px;
                            color: #409eff;
                            // 鼠标移入的效果
                            cursor: pointer;
                        }
                    }

                    // 忘记密码结束


                    // 去注册
                    .footer-go-register {
                        display: flex;
                        justify-content: center;
                        font-size: 12px;
                        margin: 12px 0;

                        // 去注册
                        .go-register {
                            font-size: 12px;
                            color: #409eff;
                            cursor: pointer;
                        }
                    }
                }


            }
        }
    }
}

// 底部外壳
.footer-wrapped {
    margin-top: 10px;

    // 底部内容
    .footer-content {
        width: 1200px;
        margin: 0 auto;
        display: flex;
        align-items: center;
        justify-content: center;

        // 底部介绍
        .title {
            color: #666;
        }

        span {
            color: #666;
            font-size: 12px;
        }
    }
}

// tabs标签
:deep(.el-tabs__item) {
    color: #333;
    font-size: 18px;
}

// 输入框高度
:deep(.el-input__inner) {
    height: 40px;
}

// 输入框标签字体高度
:deep(.el-form-item__label) {
    height: 40px;
    line-height: 40px;
}

// 登录按钮
:deep(.el-button) {
    width: 300px;
    height: 45px;
    font-size: 16px;
}
</style>